package com.org.cys.service.impl.system;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.org.cys.dao.base.IGeneralDao;
import com.org.cys.pojo.base.Page;
import com.org.cys.service.system.PrivilegeRoleService;
import com.org.cys.service.system.RoleService;
import com.org.cys.service.system.UserRoleService;
import com.org.cys.util.IdWorker;
import com.org.cys.util.MapUtil;
import com.org.cys.util.PageData;


/**
 *	t_system_role Service实现类
 *	@author caiyushen
 */
@Service("roleServiceImpl")
public class RoleServiceImpl implements RoleService {
  
  @Resource(name="generalDao")
  private IGeneralDao generalDao;
  
  @Resource(name = "privilegeRoleServiceImpl")
  private PrivilegeRoleService privilegeRoleService;
  
  @Resource(name = "userRoleServiceImpl")
  private UserRoleService userRoleService;
  
  public static String XMLMapper = "RoleMapper";
  
  /**
	* 装载Role
	* @param PageData
	* @return
	* @throws Exception 
	* */
  @Override
  public PageData getEntityById(PageData pd) throws Exception {
	  return (PageData) generalDao.findForObject(XMLMapper+".findById", pd);
  }
  
  /**
	* 保存Role
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void save(PageData pd) throws Exception{
	  pd.put("id", IdWorker.getId());
	  pd.put("createDate", new Date());
	  pd.put("updateDate", new Date());
	  generalDao.save(XMLMapper+".save",pd);
  }
  
  /**
   * 批量保存Role
   * @param PageData
   * @throws Exception
   * */
  public void saveBatch(List<PageData> pdList) throws Exception{
	  Date nowDateTime = new Date();
	  for(PageData pd:pdList){
		  pd.put("updateDate", nowDateTime);
	  }
	  generalDao.batchSave(XMLMapper+".saveBatch",pdList);
  }
  
  /**
	* 更新Role
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void update(PageData pd) throws Exception{
	  pd.put("updateDate", new Date());
	  generalDao.update(XMLMapper+".update",pd);
  }
  
  /**
	* 删除Role
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void delete(PageData pd) throws Exception{
	  //先删除中间表数据
	  privilegeRoleService.deleteByRoleId(MapUtil.getLong(pd,"id"));
	  userRoleService.deleteByRoleId(MapUtil.getLong(pd,"id"));
	  //再删除Role数据
	  generalDao.delete(XMLMapper+".delete", pd);
  }
  
  /**
	* Role列表
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> dataList(Page page) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".dataListPage", page);
  }
  
  /**
	* 获取所有Role数据
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> listAll(PageData pd) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".listAll", pd);
  }
  
}